package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.GDTabbedSearchDialog;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class ItemSelectionPane extends AdjustablePanel {
    private ItemTypePane pnlItemTypes;
    private ItemSetPane pnlItemSet;
    private ArmorClassPane pnlArmorClasses;
    private JComboBox cbSets;
    private ItemRarityPane pnlItemRarity;
    private ArtifactRarityPane pnlArtifactRarity;
    private LevelSelPane pnlLevel;
    private GDTabbedSearchDialog.Mode mode;

    public ItemSelectionPane(final GDTabbedSearchDialog.Mode mode) {
        this.mode = mode;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel pnlRarity = new JPanel();
        layout = new GroupLayout(pnlRarity);
        pnlRarity.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlItemSet).addComponent(this.pnlItemRarity).addComponent(this.pnlArtifactRarity).addComponent(this.pnlArmorClasses).addComponent(this.pnlLevel));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlItemSet)).addGroup(layout.createParallelGroup().addComponent(this.pnlItemRarity)).addGroup(layout.createParallelGroup().addComponent(this.pnlArtifactRarity)).addGroup(layout.createParallelGroup().addComponent(this.pnlArmorClasses)).addGroup(layout.createParallelGroup().addComponent(this.pnlLevel));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.pnlItemSet, this.pnlItemRarity);
        layout.linkSize(0, this.pnlItemSet, this.pnlArtifactRarity);
        layout.linkSize(0, this.pnlItemSet, this.pnlArmorClasses);
        layout.linkSize(0, this.pnlItemSet, this.pnlLevel);
        layout.linkSize(1, this.pnlItemSet, this.pnlItemTypes);
        layout.linkSize(1, this.pnlItemSet, this.pnlArtifactRarity);
        layout.linkSize(1, this.pnlItemSet, this.pnlArmorClasses);
        layout.linkSize(1, this.pnlItemSet, this.pnlLevel);
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlItemTypes).addComponent(pnlRarity));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlItemTypes)).addGroup(layout.createParallelGroup().addComponent(pnlRarity));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, pnlRarity, this.pnlItemTypes);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_ITEM_INFO"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.pnlItemTypes == null) {
            this.pnlItemTypes = new ItemTypePane(this.mode);
        } else {
            this.pnlItemTypes.adjustUI();
        }
        if (this.pnlItemSet == null) {
            this.pnlItemSet = new ItemSetPane();
        } else {
            this.pnlItemSet.adjustUI();
        }
        if (this.pnlItemRarity == null) {
            this.pnlItemRarity = new ItemRarityPane(0);
        } else {
            this.pnlItemRarity.adjustUI();
        }
        if (this.pnlArtifactRarity == null) {
            this.pnlArtifactRarity = new ArtifactRarityPane(0);
        } else {
            this.pnlArtifactRarity.adjustUI();
        }
        if (this.pnlArmorClasses == null) {
            this.pnlArmorClasses = new ArmorClassPane(0);
        } else {
            this.pnlArmorClasses.adjustUI();
        }
        if (this.pnlLevel == null) {
            this.pnlLevel = new LevelSelPane(0);
        } else {
            this.pnlLevel.adjustUI();
        }
    }

    public void updateConfig() {
        if (this.pnlItemSet != null) {
            this.pnlItemSet.updateItemSets();
        }
    }

    public void clear() {
        this.pnlItemTypes.clear();
        this.pnlItemSet.clear();
        this.pnlArmorClasses.clear();
        this.pnlItemRarity.clear();
        this.pnlArtifactRarity.clear();
        this.pnlLevel.clear();
    }

    public void addCriteria(final SelectionCriteria criteria) {
        this.pnlItemTypes.addCriteria(criteria);
        this.pnlItemSet.addCriteria(criteria);
        this.pnlArmorClasses.addCriteria(criteria);
        this.pnlItemRarity.addCriteria(criteria);
        this.pnlArtifactRarity.addCriteria(criteria);
        this.pnlLevel.addCriteria(criteria);
        criteria.selMode = SelectionCriteria.SelectionMode.ITEM;
    }
}
